package com.ruoyi.web.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.domain.OldNews;
import com.ruoyi.web.domain.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.OldQuestionMapper;
import com.ruoyi.web.domain.OldQuestion;
import com.ruoyi.web.service.IOldQuestionService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 问答Service业务层处理
 * 
 * @author drx
 * @date 2024-03-14
 */
@Service
public class OldQuestionServiceImpl implements IOldQuestionService
{
    @Autowired
    private OldQuestionMapper questionMapper;


    /**
     * 查询问答
     * 
     * @param id 问答主键
     * @return 问答
     */
    @Override
    public OldQuestion selectQuestionById(Long id)
    {
        return questionMapper.selectQuestionById(id);
    }

    public AjaxResult getOldQuestionById(Long id)
    {
        {
            AjaxResult ajaxResult=new AjaxResult();
            try{
                OldQuestion question = questionMapper.selectQuestionById(id);
                if(null != question){
                    //状态为未启用时，返回空对象
                    if("0".equals(question.getState())){
                        ajaxResult.put("data",null);
                        ajaxResult.put("code","1");
                        ajaxResult.put("msg","查询成功！");
                        return  ajaxResult;
                    }
                }
                ajaxResult.put("data",question);
                ajaxResult.put("code","1");
                ajaxResult.put("msg","查询成功！");
            } catch (Exception e){

                e.printStackTrace();
                ajaxResult.put("code","500");
                ajaxResult.put("msg","获取药品详情失败，请联系管理员！");
            }
            return ajaxResult;
        }
    }

    @Override
    public AjaxResult getUpandDown(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<HashMap<String, Object>> upandDown = questionMapper.getUpandDown(id);
            ajaxResult.put("data", upandDown);
            ajaxResult.put("code", "200");
            ajaxResult.put("msg", "查询成功！");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.put("code", "500");
            ajaxResult.put("msg", "获取问答上下篇失败，请联系管理员！");
        }
        return ajaxResult;
    }

    @Override
    public void updateQuesBrowseNum(Long id){
        questionMapper.updateQuesBrowseNum(id);
    }


    /**
     * 查询问答列表
     * 
     * @param question 问答
     * @return 问答
     */
    @Override
    public List<OldQuestion> selectQuestionList(OldQuestion question)
    {
        return questionMapper.selectQuestionList(question);
    }

    /**
     * 新增问答
     * 
     * @param question 问答
     * @return 结果
     */
    @Override
    public int insertQuestion(OldQuestion question)
    {
        question.setCreateTime(DateUtils.getNowDate());
        question.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        question.setCreateName(ShiroUtils.getSysUser().getUserName());
        question.setUpdateTime(DateUtils.getNowDate());
        question.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        question.setUpdateName(ShiroUtils.getSysUser().getUserName());
        return questionMapper.insertQuestion(question);
    }

    /**
     * 修改问答
     * 
     * @param question 问答
     * @return 结果
     */
    @Override
    public int updateQuestion(OldQuestion question)
    {
        question.setUpdateTime(DateUtils.getNowDate());
        question.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        question.setUpdateName(ShiroUtils.getSysUser().getUserName());
        return questionMapper.updateQuestion(question);
    }

    /**
     * 批量删除问答
     * 
     * @param ids 需要删除的问答主键
     * @return 结果
     */
    @Override
    public int deleteQuestionByIds(String ids)
    {
        return questionMapper.deleteQuestionByIds(Convert.toStrArray(ids),ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 删除问答信息
     * 
     * @param id 问答主键
     * @return 结果
     */
    @Override
    public int deleteQuestionById(Long id)
    {
        return questionMapper.deleteQuestionById(id,ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 修改问题发布状态
     *
     * @param question
     * @return 结果
     */
    @Transactional
    public int updateOldQuestionState(OldQuestion question)
    {
        question.setUpdateTime(DateUtils.getNowDate());
        question.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        question.setUpdateName(ShiroUtils.getSysUser().getUserName());
        return questionMapper.updateQuestion(question);
    }

    /**
     * 获取药品页旧新闻视图
     *
     * @return AjaxResult
     */
    public AjaxResult getDrugOldQuestionView(){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            List<OldQuestion> list= questionMapper.getDrugOldQuestionView();
            ajaxResult.put("data",list);
            ajaxResult.put("code", Constants.SUCCESS);
            ajaxResult.put("msg","查询成功！");
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",Constants.FAIL);
            ajaxResult.put("msg","获取问答视图失败，请联系管理员！");
        }
        return ajaxResult;
    }
}
